<section class="content-header">
    <ol class="breadcrumb">
        <li><a href="/luckydog/index"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li>账号管理</li>
        <li><a href="/role/list">角色管理</a></li>
        <li class="active"><?php if($gropuID){echo "编辑角色";}else{echo "添加角色";}?></li>
    </ol>
</section>
<style type="text/css">
    input[type="checkbox"], input[type="radio"] {
    cursor: pointer;
    height: 15px;
    left: -9999px;
    opacity: 1;
    position: static;
    width: 15px;
    z-index: 12;
}
</style>
<section class="content">
      <div class="row">
        <div class="col-md-12">
          <div class="box">
            <!-- /.box-header -->
            <div class="box-body">
                <div class="form-group pull-left" style="width: 150px;margin-left: 10px;">
                    <input type="text" value="<?= isset($roleInfo['name'])?$roleInfo['name']:''?>" class="form-control" id="rolename" placeholder="请输入角色名称">
                </div>
                <div class="row">
                    <form  action="/" method="POST" id="theForm"  target="_parent" style="padding:20px; 20px;" >
                        <?php foreach ($roleList as $key => $menu):?>
                        <table width="90%"  height="30" border="0" cellpadding=0 cellspacing=0  class="table table-border">
                            <tr>
                                <td>
                                    <table width="90%"   cellpadding=0 cellspacing=0 class="noborder"  >
                                        <tr>
                                            <td height="26" valign="center"  rowspan="2" scope="col" class="Ziyuan_next" >
                                                <input type="checkbox" class="title_td" <?php if(isset($roleInfo['power'][$menu['role']])):?> checked="checked"<?php endif;?>   name="<?= $menu['role']?>"  ><?= $menu['name']?>
                                            </td>
                                            <?php foreach ($menu['child'] as $childkey => $child):?>
                                                <?php if($childkey <= 1):?>
                                                <td class="actClass"><input  <?php if(isset($roleInfo['power'][$menu['role']]) && in_array($child['role'],$roleInfo['power'][$menu['role']])):?> checked="checked"<?php endif;?> type="checkbox" name="<?= $child['role']?>"class="<?= $menu['role']?>_next"  ><?= $child['name']?></td>
                                                <?php endif;?>
                                            <?php endforeach;?>
                                        </tr>
                                        <tr>
                                            <?php foreach ($menu['child'] as $childkey => $child):?>
                                            <?php if($childkey > 1):?>
                                                <td class="actClass"><input <?php if(isset($roleInfo['power'][$menu['role']]) && in_array($child['role'],$roleInfo['power'][$menu['role']])):?> checked="checked"<?php endif;?> type="checkbox" name="<?= $child['role']?>"class="<?= $menu['role']?>_next"  ><?= $child['name']?></td>
                                            <?php endif;?>
                                            <?php endforeach;?>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                        <?php endforeach;?>
                        <input type="hidden" value="<?= $gropuID?>" id="gropuID" />
                    </form>
                    <table width="90%" style="margin-left: 20px;">
                        <tr>
                            <td class="none">
                                <input type="checkbox" class="all" />全选
                                <input type="button" class="btn btn-success submit" value="保存" />
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
          </div>
        </div>
    </div>
</section>
<script type="text/javascript">
    $(document).ready(function(){
        $('.all').click(function(){
            if($(this).prop("checked")){
                $("#theForm").find("input[type='checkbox']").prop('checked',true);
            }else{
                $("#theForm").find("input[type='checkbox']").removeProp('checked');
            }
        });

        $('.title_td').on("click", function(){
            var _this_table = $(this).parent().parent().parent();
            if($(this).prop("checked")){
                _this_table.find("input[type='checkbox']").prop("checked",true);
            }else{
                _this_table.find("input[type='checkbox']").removeProp("checked");
            }
        });

        $('.submit').click(function(){
            var rolename = $('#rolename').val();
            var gropuID = $('#gropuID').val();
            if(rolename == ""){
                layer.msg("请添加角色名");return false;
            }
            var powerstr  = "";
            var stip = "";
            $('.title_td').each(function(){
                if(true == $(this).prop("checked")){
                    powerstr += stip + $(this).attr('name')+":"+ find_next( $(this).attr('name') );
                    stip = "|";
                }
            })
            if(confirm("确定要编辑角色吗？"))
            {
                $.post("/role/systemPowerSet",{powerstr:powerstr,rolename:rolename,gropuID:gropuID},function(data)
                {
                    layer.msg(data.message);
                    window.location.href="/role/list";
                },"json");
            }
        });
        function find_next( tip ){
            var cla = "."+tip+"_next";
            var str = ''; var stip = "";
            $(cla).each(function(){
                if( true == $(this).prop("checked")){
                    str += stip + $(this).attr('name')+"*";
                    stip = "-";
                }
            })
            return str;
        }
    })
    
</script>





